package com.myblog.utils;

import org.springframework.util.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.security.MessageDigest;

/**
 * @author: Jie
 * @Description: 转码 & 加密 MD5
 * @Date: 2019/12/3
 */
public class SecureUtils {

    public static void main(String[] argr) {
//        System.out.println(unicodeToString("\\ufeff"));
        //4297f44b13955235245b2497399d7a93
        String a = "123123";
        String s = MD5(a);
        String b = "4297F44B13955235245B2497399D7A93";
        System.out.println("S0:"+s);
        System.out.println(s.equals(b));
        System.out.println(!s.equals(b));
    }

    /**----------------------------------------------加 密-------------------------------------------------**/

    private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

    private static String byteArrayToHexString(byte b[]) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++)
            resultSb.append(byteToHexString(b[i]));

        return resultSb.toString();
    }

    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0)
            n += 256;
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    /**
     *  加密网址 ： http://tool.chinaz.com/tools/md5.aspx
     *  解密 ： https://www.cmd5.com/
     *  MD5 加密
     */
    public static String MD5(String origin) {
        String resultString = MD5Encode(origin,"UTF-8");
//        if(StringUtils.isNotBlank(resultString)){
//            return resultString.toUpperCase();
//        }
        return resultString;
    }

    /**
     * MD5 比较 匹配origin 加密后是否等于md5(通常用于修改密码)
     * @param origin 密码， 未加密
     * @param md5 已加密字符串
     * @return
     */
    public static boolean compareMD5(String origin,String md5) {
        String result =MD5(origin);
        return md5.equals(result);
    }

    /**
     *  MD5 加密
     */
    public static String MD5Encode(String origin, String charsetName) {
        String resultString = null;
        try {
            resultString = new String(origin);
            MessageDigest md = MessageDigest.getInstance("MD5");
            if (charsetName == null || "".equals(charsetName))
                resultString = byteArrayToHexString(md.digest(resultString
                        .getBytes()));
            else
                resultString = byteArrayToHexString(md.digest(resultString
                        .getBytes(charsetName)));
        } catch (Exception exception) {

        }
        return resultString;
    }


    /**----------------------------------------------转 码-------------------------------------------------**/

    /**
     * 中文转unicode编码
     * @param gbString 汉字
     * @return unicode编码
     */
    public static String stringToUnicode(final String gbString){
        char[] utfBytes = gbString.toCharArray();
        String unicodeBytes = "";
        for (int i = 0; i < utfBytes.length; i++){
            String hexB = Integer.toHexString(utfBytes[i]);
            if (hexB.length() <= 2){
                hexB = "00" + hexB;
            }
            unicodeBytes = unicodeBytes + "\\u" + hexB;
        }
        return unicodeBytes;
    }

    /**
     * unicode编码转中文
     * @param dataStr unicode编码
     * @return 中文
     */
    public static String unicodeToString(final String dataStr){
        int start = 0;
        int end = 0;
        final StringBuffer buffer = new StringBuffer();
        while(start > -1){
            end = dataStr.indexOf("\\u", start + 2);
            String charStr = "";
            if(end == -1){
                charStr = dataStr.substring(start + 2, dataStr.length());
            }else{
                charStr = dataStr.substring(start + 2,end);
            }
            //16进制parse整形字符串
            char letter = (char) Integer.parseInt(charStr, 16);
            buffer.append(new Character(letter).toString());
            start = end;
        }
        return buffer.toString();
    }

    /**
     * 将utf-8展开的16进制数转换成utf-8汉字
     * @param strUtf16
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String utf16ToUtf8(String strUtf16) throws UnsupportedEncodingException{
        String strUtf8 = URLDecoder.decode(strUtf16, "UTF-8");
        return strUtf8;
    }




}
